const basePage = require('../../../common/BasePage')
Page({
  behaviors: [basePage],
  data: {
    status: 1,
    list: [{
        text: '可使用',
        key: 1,
      },
      {
        text: '已使用',
        key: 0,
      },

    ],

    couponList: [],
    goodsSid: '',
  },

  onLoad(obj) {
    console.log('onload', obj);
    this.data.goodsSid = obj.goodsSid || ''
    this.init();
  },

  init() {
    this.fetchList();
  },

  fetchList(status = this.data.status) {
    console.log('fetchList', status);
    this.request({
      url: this.data.host + "/v1/coupons/able",
      data: {
        used: status,
        size: 50,
        goodsId: this.data.goodsSid
      }
    }, res => {
      console.log('优惠券', res);
      res.data.records.map(val => {
        val.reduceLimit = (val.reduceLimit / 100).toFixed(2)
      })
      this.setData({
        couponList: res.data.records
      });
    })
    // let statusInFetch = '';
    // switch (Number(status)) {
    //   case 0: {
    //     statusInFetch = 'default';
    //     break;
    //   }
    //   case 1: {
    //     statusInFetch = 'useless';
    //     break;
    //   }
    //   case 2: {
    //     statusInFetch = 'disabled';
    //     break;
    //   }
    //   default: {
    //     throw new Error(`unknown fetchStatus: ${statusInFetch}`);
    //   }
    // }
    // fetchCouponList(statusInFetch).then((couponList) => {
    //   this.setData({ couponList });
    // });
  },
  pick_click(e) {
    console.log('pick_click', e);
    this.request({
      url: this.data.host + "/v1/coupons/pick-up",
      data: {
        sid: e.currentTarget.dataset.item.sid
      },
      method: "post"
    }, res => {
      wx.showToast({
        title: '领取成功',
      })
      this.data.couponList.splice(e.currentTarget.dataset.index, 1);
      this.setData({
        couponList: this.data.couponList
      })
    })
  },
  tabChange(e) {
    const {
      value
    } = e.detail;

    this.setData({
      status: value
    });
    this.fetchList(value);
  },

  goCouponCenterHandle() {
    // wx.showToast({
    //   title: '去领券中心',
    //   icon: 'none'
    // });
    wx.navigateTo({
      url: '/pages/coupon/coupon-list/pick',

    })
  },

  onPullDownRefresh_() {
    this.setData({
        couponList: [],
      },
      () => {
        this.fetchList();
      },
    );
  },
});